package no.gnet.edvd.web.controller;

import static no.gnet.edvd.web.util.Constants.EVENTODDSMAP_;
import static no.gnet.edvd.web.util.Constants.SESSIONUSER;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import no.gnet.edvd.DataStore;
import no.gnet.edvd.engines.Engine;
import no.gnet.edvd.engines.EventOdds;
import no.gnet.edvd.lightweight.CompactBet;
import no.gnet.edvd.lightweight.EmailAndP2PHolder;
import no.gnet.edvd.types.ActivityType;
import no.gnet.edvd.types.Bet;
import no.gnet.edvd.types.Player;
import no.gnet.edvd.web.cache.MemCache;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ProfileController extends CommonController {

	private static final String USERBETS = "userbets";
	private static final String ODDSFORUSERBETS = "oddsforuserbets";

	private static Logger logger = Logger.getLogger(ProfileController.class);

	@Autowired
	DataStore datastore;

	@Autowired
	Engine engine;

	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public String register(ModelMap model, HttpServletRequest request) {
		logger.debug("profile requested for user");
		List<Bet> userbets = datastore.getUserBets(sessionUser(request).getPlayer().id);
		List<String> oddsForUserBets=null;
		try {
			oddsForUserBets = getOddsForUserBets(userbets);
		} catch (Exception e) {
			logger.error("Cache empty?");
			refreshEventDescriptors(datastore, engine, request);
		}
		model.addAttribute(USERBETS, userbets);
		model.addAttribute(ODDSFORUSERBETS, oddsForUserBets);
		model.addAttribute(SESSIONUSER, sessionUser(request));
		return "profile";
	}



	private List<String> getOddsForUserBets(List<Bet> ubets) {
		List<String> eodds = new ArrayList<String>();
		for (Iterator iterator = ubets.iterator(); iterator.hasNext();) {
			Bet bet = (Bet) iterator.next();
			EventOdds e = (EventOdds)MemCache.get(EVENTODDSMAP_+bet.event.id);
			if(e!=null)
				eodds.add(String.valueOf(e.get(bet.option)));
			else{
				throw new IllegalStateException("Cache needs to be populated here");
			}
		}
		return eodds;
	}
	
	@RequestMapping(value = "/setppe", method = RequestMethod.POST, produces = "application/json", headers = { "Content-type=application/json" })
	public @ResponseBody String setppe(@RequestBody EmailAndP2PHolder holder,
			HttpServletRequest request) throws ServletException {
		Player player = sessionUser(request).getPlayer();
		String mail = holder.getEmail();
		if(holder.isUsesame()){
			mail=player.getEmail();
		}
		logger.info("Setting ppe for user " + player.id+", email is " + mail+", usesame is " + holder.isUsesame() + ", p2p is " + holder.isP2p());
		datastore.setPaymentAccountForUser(player.id, mail);
		datastore.setPaymode(player.id, holder.isP2p());
		markSessionDirty(request);
		return "OK";
	}
	
}
